<?php
namespace GuessFilm\Controller;
use Common\Controller\GuessFilmController;
class TestController extends GuessFilmController {

    public function index(){

        $json_string = file_get_contents("./public/data/data.json");

      // 用参数true把JSON字符串强制转成PHP数组
      $data = json_decode($json_string, true);
      var_dump($data);
    }

    /**
     * 抓取网络数据
     */
    public function getfile(){

        ignore_user_abort(); //php一直运行


        for ($i=1;$i<=103;$i++){

            $url = "https://ossimg.zjrvip.cn/player_head/$i.png";

            $jiequ = substr($url,strrpos($url,'/')+1);
            $user_path = 'Public/data/fott';
            $this->getImage($url,$user_path,$jiequ);

        }

        echo 'ok';
        exit('ok');
        //$this->getImage($url,$user_path,'f817c6558554f457cb2f97581e77eef6.mp4');

    }



    public function cache(){

        exit;
        $question =  M('guessflim_questions')->select();

        foreach ($question as $key=>$value){

           $data = array(
               'app'=>$value['app'],
               'third_id'=>$value['third_id'],
               'question'=>$value['question'],
               'question_type'=>$value['question_type'],
               'video_url'=>$value['video_url'],
               'answer'=>$value['answer'],
               'choice'=>$value['choice'],
               'tips'=>$value['tips'],
               'customs'=>$key+1
           );

            M('guessflim_questions2')->data($data)->add();

        }

        var_dump($question);
    }

    public function yibu(){

        exit;
        error_reporting(E_ALL);

        $start_task = 1;

        $num = 2000;
        $start = (10*$num)*$start_task;

        $handle1 = popen('php index.php /GuessFilm/Test/getfile', 'r');
//$handle2 = popen('php index.php /Message/Task/index', 'r');
//$handle3 = popen('php index.php /Message/Task/index', 'r');
//$handle4 = popen('php index.php /Message/Task/index', 'r');
//$handle5 = popen('php index.php /Message/Task/index', 'r');
//$handle6 = popen('php index.php /Message/Task/index', 'r');
//$handle7 = popen('php index.php /Message/Task/index', 'r');
//$handle8 = popen('php index.php /Message/Task/index', 'r');
//$handle9 = popen('php index.php /Message/Task/index', 'r');
//$handle10 = popen('php index.php /Message/Task/index', 'r');

        echo "'$handle1'; " . gettype($handle1) . "\n";
//echo "'$handle2'; " . gettype($handle2) . "\n";
//echo "'$handle3'; " . gettype($handle3) . "\n";
//echo "'$handle4'; " . gettype($handle4) . "\n";
//echo "'$handle5'; " . gettype($handle5) . "\n";
//echo "'$handle6'; " . gettype($handle6) . "\n";
//echo "'$handle7'; " . gettype($handle7) . "\n";
//echo "'$handle8'; " . gettype($handle8) . "\n";
//echo "'$handle9'; " . gettype($handle9) . "\n";
//echo "'$handle10'; " . gettype($handle10) . "\n";

//sleep(20);
        while(!feof($handle1) ){
            $read = fgets($handle1);
            echo $read;

//    $read = fgets($handle2);
//    echo $read;
//
//    $read = fgets($handle3);
//    echo $read;
//
//    $read = fgets($handle4);
//    echo $read;
//
//    $read = fgets($handle5);
//    echo $read;
//
//    $read = fgets($handle6);
//    echo $read;
//
//    $read = fgets($handle7);
//    echo $read;
//
//    $read = fgets($handle8);
//    echo $read;
//
//    $read = fgets($handle9);
//    echo $read;
//
//    $read = fgets($handle10);
//    echo $read;
        }



        pclose($handle1);
    }



    public function jiequ(){
        var_dump(S('updateq'));
        $url = "https://dl.weshineapp.com/gif/20180210/f817c6558554f457cb2f97581e77eef6.mp4";
        $str = substr($url,strrpos($url,'/')+1);
        var_dump($str);
    }

    /**
     * 获取网络文件
     * @param $url
     * @param string $save_dir
     * @param string $filename
     * @param int $type
     * @return array
     */
    private function getImage($url,$save_dir='',$filename='',$type=0){
        if(trim($url)==''){
            return array('file_name'=>'','save_path'=>'','error'=>1);
        }
        if(trim($save_dir)==''){
            $save_dir='./';
        }
//        if(trim($filename)==''){//保存文件名
//            $ext=strrchr($url,'.');
//            if($ext!='.gif'&&$ext!='.jpg'){
//                return array('file_name'=>'','save_path'=>'','error'=>3);
//            }
//            $filename=time().$ext;
//        }
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
        if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
            return array('file_name'=>'','save_path'=>'','error'=>5);
        }
        //获取远程文件所采用的方法
        if($type){
            $ch=curl_init();
            $timeout=5;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $img=curl_exec($ch);
            curl_close($ch);
        }else{
            ob_start();
            readfile($url);
            $img=ob_get_contents();
            ob_end_clean();
        }
        //$size=strlen($img);
        //文件大小
        $fp2=@fopen($save_dir.$filename,'a');
        fwrite($fp2,$img);
        fclose($fp2);
        unset($img,$url);
        return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
    }

}